package com.bwie.product.service.impl;

import com.bwie.common.core.domain.Result;
import com.bwie.product.domain.po.PmsSkuImages;
import com.bwie.product.domain.po.PmsSkuInfo;
import com.bwie.product.domain.po.PmsSpuInfoDesc;
import com.bwie.product.domain.vo.AttrGroupVo;
import com.bwie.product.domain.vo.ProductVo;
import com.bwie.product.domain.po.SaleAttr;
import com.bwie.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ProductServiceImpl implements ProductService {

   @Autowired
   private PmsSkuInfoService pmsSkuInfoService;
   @Autowired
   private PmsSkuImagesService pmsSkuImagesService;
   @Autowired
   private PmsSpuInfoDescService pmsSpuInfoDescService;
   @Autowired
   private SaleAttrVoService saleAttrVoService;
   @Autowired
   private AttrGroupVoService attrGroupVoService;


    /**
     * sku商品详情
     * @param skuId
     * @return
     */
    @Override
    public Result productDetail(Long skuId) {
        ProductVo productVo = new ProductVo();

        //查sku详情
        CompletableFuture<PmsSkuInfo> skuInfo = CompletableFuture.supplyAsync(()->{
            Result<PmsSkuInfo> pmsSkuInfoResult = pmsSkuInfoService.selectSkuInfoById(skuId);
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoResult.getData();
            productVo.setPmsSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        });

        //查sku图片信息
        CompletableFuture<List<PmsSkuImages>> pmsSkuImages = CompletableFuture.supplyAsync(() -> {
            Result<List<PmsSkuImages>> listResult = pmsSkuImagesService.selectSkuImagesList(skuId);
            List<PmsSkuImages> imagesList = listResult.getData();
            productVo.setSkuImagesList(imagesList);
            return imagesList;
        });


        //spu信息介绍
        CompletableFuture<PmsSpuInfoDesc> pmsSpuInfoDescCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<PmsSpuInfoDesc> pmsSpuInfoDescResults = pmsSpuInfoDescService.selectPmsSpuInfoDescList(skuId);
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescResults.getData();
            productVo.setSpuInfoDesc(pmsSpuInfoDesc);
            return pmsSpuInfoDesc;
        });

        //根据spuId查询销售属性
        CompletableFuture<List<SaleAttr>> listCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<List<SaleAttr>> listResult = saleAttrVoService.selectSaleAttrVoList(skuInfo.join().getSpuId());
            List<SaleAttr> saleAttrList = listResult.getData();
            productVo.setSaleAttrs(saleAttrList);
            return saleAttrList;
        });

        //查规格和包装 05s
        CompletableFuture<List<AttrGroupVo>> AttrVoList = CompletableFuture.supplyAsync(() -> {
            Result<List<AttrGroupVo>> groupVoList = attrGroupVoService.selectAttrGroupVoList(skuInfo.join().getCatalogId());
            List<AttrGroupVo> attrGroupVoList = groupVoList.getData();
            productVo.setAttrGroupVos(attrGroupVoList);
            return attrGroupVoList;
        });

        CompletableFuture.allOf(skuInfo,pmsSkuImages,pmsSpuInfoDescCompletableFuture,listCompletableFuture,AttrVoList).join();

        return Result.success(productVo);
    }
}
